<?php
function create_dir($dirName)
{
    $dirs = explode('/', $dirName);
    $dir  = '';
    foreach ($dirs as $part) {
        $dir .= $part.'/';
        if (!is_dir($dir) && strlen($dir) > 0)
            mkdir($dir, 0777);
            //chmod($dir, 0777);
    }
}

function get_all_cat()
{
	$q	=	mysql_query("SELECT * FROM phanloai ORDER BY pl_title ASC");
	$cats	=	array();
	while($row	=	mysql_fetch_array($q)){
		$cat['pl_id']	=	$row['pl_id'];
		$cat['pl_title']	=	$row['pl_title'];	
		$cats[]	=	$cat;	
	}
	return	$cats;
}

function check_login()
{
	if(isset($_SESSION['_user'])){
		$user	=	$_SESSION['_user'];
		$q	=	mysql_query("SELECT * FROM thanhvien WHERE t_user='".$user['username']."' AND t_pass='".$user['password']."' AND t_hoat_dong=1");
		$num	=	mysql_num_rows($q);
		if($num == 1)return TRUE;
	}
	return FALSE;
}

function get_mem_info($id)
{
	$q	=	mysql_query("SELECT * FROM thanhvien WHERE t_id=".intval($id));
	$num	=	mysql_num_rows($q);
	if($num ==1 )return	mysql_fetch_array($q);
	return false;
}

function pagination($url = '', $num_rows = 100, $limit = 10, $cur_page = 1, $show_num = 'yes'){
	$pagination = '';
	$total = ceil($num_rows/$limit);
	if($total > 1){
		$first = 0;
		$end = 0;
		if ($cur_page != 1) 
		{
			$first = $cur_page - 3;
			if ($first<=0) {$first = 1;$end = 4 -  $cur_page;}
			$end = $end + $cur_page + 3;
			if ($end > $total) 
			{
				$end = $total;
				$first = $total - 6;
				if ($first <= 0) {$first = 1;}
			}
		}
		else {
			$first = 1;$end = 7;
			if ($end > $total) {$end = $total;}
		}
		
		$pagination.=	"<div id='pagnav'>";
		$pagination.= "<ul>";
			
			if($cur_page>1)
			{
				$pagination .=	"<li>";
				if(($cur_page-1)==1)
					$pagination .= "<a class='' href='".$url.(strpos($url,'?') ? '&' : '?')."p=1"."'>Trang trước</a>";
				else
					$pagination .= "<a class='' href='".$url.(strpos($url,'?') ? '&' : '?')."p=". ($cur_page-1) ."'>Trang trước</a>";
				$pagination .=	"</li>";
			}
			
			if($show_num == 'yes'){
				$j=0;
				for($i = $first; $i <= $end; $i++)
				{
					$j++;
					if($i==$cur_page)
					{
						$pagination .= "<li>";
						$pagination .= '<a class="">'.$i.'</a>';
						$pagination .= "</li>";
					}
					else
					{
						$pagination .= "<li>";
						if($i==1)
							$pagination .= "<a title='' class='' href='".$url.(strpos($url,'?') ? '&' : '?')."p=1"."'>".$i."</a>";
						else
							$pagination .= "<a title='' class='' href='".$url.(strpos($url,'?') ? '&' : '?')."p=".$i."'>".$i."</a>";
						$pagination .= "</li>";
					}
					
				}
			}else{
				if($cur_page > 1 && $cur_page < $total)
					$pagination .= "";
			}
			
			
			
			if($cur_page<$total)
			{
				$pagination.= "<li>";
				$pagination.=	"<a class='' href='".$url.(strpos($url,'?') ? '&' : '?')."p=".($cur_page+1)."'>Trang sau</a>";
				$pagination.= "</li>";
			}
			
			
		$pagination.= "</ul>";
		$pagination.= "</div>";
	}	
	return $pagination;
}